Skip to content

Fix soundness bugs found by the coherence audit - #25

Open
evertonfraga wants to merge 15 commits into
test/per-obligation-goldensfrom
fix/soundness-audit
Open

evertonfraga wants to merge 15 commits into
test/per-obligation-goldensfrom
fix/soundness-audit

Conversation

@evertonfraga

@evertonfraga evertonfraga commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes the soundness findings of the coherence audit that affect main: each made a false property verify. Stacked on #23 (the new fixtures use its per-obligation goldens); review after it.

One commit per fix. Each commit adds a fixture under test/examples/ that pairs true and false properties, with // Passes / // Refuted / // Not proven comments checked by the golden suite. Before each fix, the fixture's false properties were proven (✅), or the file was accepted where it is now rejected.

Translator

Commit Change Fixture
fix(translator): reject msg.sender and msg.value in invariants Rejected, as block.* already was. An invariant holds between transactions; one over the caller was established for one caller and assumed for the next. invariant_msg_sender.sol, invariant_msg_value.sol, invariant_block_value.sol
fix(translator): keep the effects of require's message arguments require(c, f()) and require(c, E(f())) evaluate the message's effects before assuming c. require_message_effects.sol
fix(translator): a bitwise result has the type of the operation An uninterpreted &, |, ^, shift by a variable (and op=) is wrapped into its type, so a signed result can be negative. ~x is exact. bitwise_types.sol
fix(translator): a constant expression has the value solc computed A literal, unary or binary expression of type int_const N translates to N: (7 / 2) * 2 is 7, not 6. Also accepts 2.5 ether and 1e18, which failed as invalid literals. constant_expressions.sol
fix(translator): in unchecked, signed division and constant powers wrap type(int8).min / -1 and B ** 2 (B a named constant) wrap within their type in unchecked. The division keeps its own source location under the wrap. unchecked_wrapping.sol
fix(translator): reject mapping and array storage parameters Such a parameter refers to the caller's storage; it was a local copy, so writes through it were lost. storage_param_mapping.sol, storage_param_array.sol
fix(translator): reject calls of functions that may end the transaction A halt (assembly return/stop, selfdestruct) translates to a labelled block. A call of a function containing one is rejected: the caller continued after it. A halt in the entry point itself is modelled as before. halt_in_helper.sol, selfdestruct_in_helper.sol
fix(translator): copy structs where Solidity copies them Where solc's types show a change of data location (assignment, declaration, internal-call argument, returned value), the struct is copied into a new object, nested structs included. A memory struct passed to an internal function has unknown fields after the call; the callee's heap frame does not cover it. A returned tuple holding a copied struct is rejected. struct_copies.sol
fix(translator): a read through a storage reference reads current storage Info storage d = data no longer snapshots data's fields into locals. d.f reads data's storage when the read runs. A reference returned by a call reads unknown values. storage_reference_reads.sol

Specification language

Commit Change Fixture
fix(spec): a quantified variable ranges over its declared type forall v : T :: P becomes forall v :: inRange(v) ==> P, and exists becomes inRange(v) && P, for uintN, intN, address and bytesN. quantifier_types.sol
fix(spec): / and % round toward zero, as in Solidity Specs use truncating division and modulus, like the code. They were Euclidean: -7 % 2 == 1. spec_division.sol
test(spec): round-trip the range guard of quantified variables The REQ-PBT-001 serializer strips the guard and recovers the type from its bounds. The generator draws quantified variables from more types. SpecParserTest.lean

Docs

  • docs/guide/src/integers.md, limitations.md, supported_features.md, contract_invariants.md and LIMITATIONS.md describe the new behaviour.
  • CHANGELOG.md has an entry for these fixes.

Testing

  • CI=true bash test/run_tests.sh: 68 passed, 0 failed, exit-code contract 8/8 (15 new fixtures), with z3 4.15.4 and with z3 4.8.12 (the version CI's Ubuntu image ships). No existing golden changed. Obligations that the two z3 versions decide differently are not pinned (see the last commit).
  • lake test, python3 -m pytest Tools/Solidity/test (15 passed), lake run pbt-determinism, and the docs site build all pass.
  • Each new fixture was also run against the pre-fix binary (main @ 87df510) to confirm that its false properties were proven there.

Not covered

  • A function that writes a struct which is not its own fresh copy still fails heap frame (only this contract is modified). This is audit finding A1-11, a false alarm that predates this PR; the fixtures avoid such writes.
  • Inheritance findings (A2-*) are for Verify a contract with its inherited members #21.

An invariant is assumed on entry to every external function and checked
on exit, so it must hold between transactions, whoever calls next.
`block.*` and `tx.*` were already rejected in invariants for that reason,
but `msg.sender` and `msg.value` were accepted, and an invariant over them
was established for one caller and assumed for the next. With
`invariant owner == msg.sender`, `require(msg.sender != owner)` could
never pass, so every property after it was proven: the false
`ensures x == 7` of a function that sets `x = 1` passed, even under
--strict. `invariant balances[msg.sender] <= totalSupply` was proven for
a contract that sets another account's balance above the supply.

Both are now rejected like `block.timestamp`.

test/examples/invariant_msg_sender.sol and invariant_msg_value.sol pin the
rejections; invariant_block_value.sol pins the existing rejection of block
values, which no fixture covered.
`require(cond, msg)` kept only `cond`. Solidity evaluates every argument
before the check, even when `cond` holds ("require arguments are evaluated
unconditionally"), so in `require(true, f())` or `require(c, E(f()))` the
call `f()` runs on the passing path. Dropping it proved false properties:
`ensures notes == old(notes)` passed for a function whose require message
increments `notes`, while the true `notes == old(notes) + 1` was refuted.

The message's effects are now evaluated before the assume; for a custom
error `E(args)`, the effects of its arguments (an error value itself has
none). As for any call, an effect that changes a variable another
argument reads is rejected, since solc's argument order is unspecified.

test/examples/require_message_effects.sol pairs each true property with a
false one, for string and custom-error messages.
`forall v : T :: P` and `exists v : T :: P` bound `v` over all integers,
whatever `T` was. So false existentials were proven: `exists i : uint8 ::
i == k` for every uint256 `k`, `exists v : uint256 :: v + 1 == 0`, and
true universals over a type were refuted (`forall v : uint8 :: v <= 255`).

The body now carries the type's range: `forall v :: inRange(v) ==> P` and
`exists v :: inRange(v) && P`, for every type with a range (uintN, intN,
address, bytesN). Those false existentials are now refuted, the true
universals proven.

test/examples/quantifier_types.sol pairs true and false properties for
both quantifiers over uint8, int8, uint256 and address.
A variable bound at an integer type now carries the range of its type
(`forall v : T :: P` parses to `forall v :: inRange(v) ==> P`). The
round-trip property serialized that guard as part of the body, so
re-parsing added a second one and REQ-PBT-001 failed on its first
sample. The canonical serializer now recognizes the guard, recovers the
type name from its bounds and emits only the written body.

The generator draws quantified variables from more types (`uint8`,
`int8`, `int256`, `bytes32`, `string` as well as `bool`, `address` and
`uint256`), covering guarded, unguarded and boolean binders.
An uninterpreted bitwise result (`&`, `|`, `^`, a shift by a variable,
and their `op=` forms) lay in [0, 2^256) whatever the operands' type, so a
signed result was never negative: `(a & a) >= 0` was proven for
`int8 a == -1`, as was `(a >> n) >= 0`. The result is now wrapped into
the operation's type, which maps [0, 2^256) onto every value of it.

`~x` is exact: `-x - 1` for a signed `x`, `2^N - 1 - x` for an N-bit
unsigned one (it was uninterpreted).

test/examples/bitwise_types.sol pairs each behaviour with a true and a
false property; before this change five of its false properties were
proven and four of its true ones failed.
solc evaluates constant expressions exactly, with rational intermediate
values, and records the result in the expression's type (`int_const 7`).
The translator evaluated them step by step in integers instead, so
`(7 / 2) * 2` was 6 and `assert(x == 6)` was proven. A literal, unary or
binary expression of `int_const` type now translates to that value.

This also makes fractional units and exponents work (`2.5 ether`,
`1e18`), which were rejected as invalid number literals.
…wrap

`type(int8).min / -1` is `type(int8).min` in `unchecked` code, but the
quotient stayed unbounded (128), so `c > 0` was proven. A signed `/` and
`/=` now wrap within their type in `unchecked`, like `+`, `-` and `*`.

A power of named constants (`B ** 2`, `B` a `uint8` constant) is folded
to a constant of their type; in `unchecked` it now wraps within that type
too (`B ** 2` is 0 for `B == 16`, not 256).

The division keeps its own source location under the wrap, so its
`divisor is non-zero` check stays located.
A specification's `/` and `%` were Euclidean (`-7 % 2 == 1`), while the
code's signed `/` and `%` truncate (`-7 % 2 == -1`). So
`ensures a % 2 == 0 || a % 2 == 1` was proven for every int256 `a`, and
`ensures r == a / 2` failed for `r = a / 2`. Specs now use the truncating
operations; for non-negative operands they agree with the Euclidean ones.
integers.md and LIMITATIONS.md now say that a constant expression has
solc's exact value (fractional units and exponents included), that `~x`
is exact and other bitwise results range over their type, that a signed
`/` and a power of constants wrap in `unchecked`, and that `/` and `%`
round toward zero in specifications too.
A mapping or array `storage` parameter refers to the caller's storage:
`_credit(balances, k)` with `m[k] += 1` writes `balances[k]`. Parameters
are modelled as local copies, so the write was lost, and
`ensures balances[k] == old(balances[k])` was proven for a function that
increments it. Such parameters are now rejected, as local mapping and
array storage references already were. Struct `storage` parameters are
unchanged.
Assembly `return` and `stop`, and `selfdestruct`, end the whole
transaction with the state written so far. They were modelled as a
return from the function they are in, which is right in an entry point
but not in a function another one calls: the caller continued, so after
`x = 1; _maybeStop(v); x = 2;` the postcondition `x == 2` was proven,
though `_maybeStop(7)` ends the call with `x == 1`.

A halt now translates to a labelled block, and a call of a function whose
body contains one is rejected. A halt in the entry point itself is still
modelled as before.
Solidity copies a struct into storage, and from storage or calldata into
memory; only memory-to-memory assignments share it. Structs are heap
objects in the model, and every assignment shared the object, so after
`b = a; b.value = 5` the postcondition `a.value == 5` was proven, as was
`a.value == 9` after `Info memory m = a; m.value = 9`.

Where solc's types say the struct changes data location (an assignment,
a declaration, an argument of an internal call, a returned value), the
translation now allocates a new object and copies the fields into it,
nested structs included. A returned tuple holding such a struct is
rejected.

A memory struct passed to an internal function is shared with it, and the
callee's heap frame does not cover it: its fields are now unknown after
the call, where they were assumed unchanged (`Info memory m = a;
_bump(m); assert(m.value == a.value)` was proven).
…rage

`Info storage d = data;` declared one local per field of `data`, set
when `d` was declared, so a read `d.value` returned that value even after
`data` changed: after `data = Info({value: 5, …})` or a call that writes
`data`, `ensures r == 1` was proven for `r = d.value`.

A storage reference no longer declares locals: a read `d.f` reads the
storage `d` refers to when it runs (`self#data#f`), when `d` was taken
from a path of state-variable fields. For any other reference (one
returned by a call) each read is an unknown value. Writes through a
reference stay rejected.
CI runs Ubuntu 24.04's z3 4.8.12, which left four obligations of the new
fixtures unknown that z3 4.15 decides:

- bitwise_types.sol: `(a & b) <= 254` times out; the false companion of
  `(a & b) <= 255` is now `(a & b) >= 1` (refuted by 0 & 0).
- quantifier_types.sol: 4.8.12 decides an existential only when its body
  simplifies to false. The refuted existentials are now of that form
  (`exists i : uint8 :: i > 255`, `exists v : uint256 :: v < 0`), and the
  true one with a witness (`exists i : uint8 :: i == k` for k <= 255) is
  dropped.

The golden suite passes under CI=true with both versions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant